#include "esp_sntp.h"
#include "http_flie_server.h"
#include "time_sntp_driver.h"
#include "camera_driver.h"
#include "fatfs_driver.h"
#include "szp_hardware_driver.h"
#include "nvs_driver.h"
#include "my_ui_api.h"
#include "actions.h"
#include "ui.h"
#include "screens.h"
#include "vars.h"
#include "fonts.h"
#include "styles.h"

static const char *TAG = "ui_action";

extern TaskHandle_t task_wifi_scan_handle;
extern TaskHandle_t task_camera_get_handle, task_camera_show_handle, task_take_photo_handle;
static const char *file_photo_quality_config = MOUNT_POINT "/config/photo_quality_config";
static const char *file_photo_pixel_config = MOUNT_POINT "/config/photo_pixel_config";

void print_heap_info() {
    printf("Internal RAM : %d bytes free\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
    printf("SPIRAM       : %d bytes free\n", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
    printf("DMA-capable  : %d bytes free\n", heap_caps_get_free_size(MALLOC_CAP_DMA));
}

void action_menu_button_clicked(lv_event_t *e)
{
    vTaskSuspend(task_camera_get_handle);
    vTaskSuspend(task_camera_show_handle);
    vTaskSuspend(task_take_photo_handle);

    ESP_LOGI(TAG, "disable camera_tasks take_photo");
    eez_flow_set_screen(SCREEN_ID_PAGE_1, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0);
}

void action_back_button_clicked(lv_event_t *e)
{
    if (get_var_save__button_statue() == false)
    {
        eez_flow_set_screen(SCREEN_ID_SAVE_SETTING_WARN_MSEEAGE, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0);
        ESP_LOGI(TAG, "jump to warn page");
    }
    else
    {
        vTaskResume(task_camera_get_handle);
        vTaskResume(task_camera_show_handle);
        vTaskResume(task_take_photo_handle);

        eez_flow_set_screen(SCREEN_ID_HOME, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0);
        ESP_LOGI(TAG, "jump to home page");
    }
}

void action_tinyusb_switch_clicked(lv_event_t *e)
{
    if (get_var_use_tinyusb() == true)
    {
        ESP_LOGI(TAG, "TinyUSB is starting");
        tinyusb_start();

        lv_obj_t *obj = lv_msgbox_create(objects.page_1, "", "", 0, true);
        lv_obj_set_pos(obj, 70, 67);
        lv_obj_set_size(obj, 209, 121);
        {
            lv_obj_t *parent_obj = obj;
            {
                lv_obj_t *obj = lv_label_create(parent_obj);
                lv_obj_set_style_text_font(obj, &ui_font_cn, LV_PART_MAIN | LV_STATE_DEFAULT);
                lv_obj_set_pos(obj, -87, -227);
                lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
                lv_label_set_text(obj, "成功挂载虚拟U盘,请将相机通\n过Type-C线缆连接设备。");
                lv_obj_set_style_align(obj, LV_ALIGN_DEFAULT, LV_PART_MAIN | LV_STATE_DEFAULT);
            }
        }
    }
    else
    {
    }
}

void action_reboot_button_clicked(lv_event_t *e)
{
    char buf[10];
    ESP_ERROR_CHECK(fatfs_write_file(file_photo_quality_config, itoa(get_var_photo_quality(), buf, 10)));
    ESP_ERROR_CHECK(fatfs_write_file(file_photo_pixel_config, itoa(get_var_select_pixel(), buf, 10)));
    save_time_into_file();
    nvs_write("mode_space","mode_key", 1);
    nvs_deinit();
    vTaskDelay(pdMS_TO_TICKS(100));
    esp_restart();
}

void task_wifi_connect(void *arg)
{
    char *ssid_str = lv_label_get_text(objects.wlan_name_label);
    char *passwd_str = lv_textarea_get_text(objects.wifi_passwd_text);
    ESP_LOGI(TAG, "connect to ssid = %s, passwd = %s", ssid_str, passwd_str);
    wifi_connect((uint8_t *)ssid_str, (uint8_t *)passwd_str);
    vTaskDelete(NULL);
}

void action_connect_wifi_button_clicked(lv_event_t *e)
{
    if (task_wifi_scan_handle != NULL)
    {
        vTaskDelete(task_wifi_scan_handle);
        lv_obj_clean(objects.wifi_list);
        // vTaskSuspend(task_wifi_scan_handle);
        task_wifi_scan_handle = NULL;
    }

    TaskHandle_t task_wifi_connect_handle;
    if (xTaskCreate(task_wifi_connect, "task_wifi_connect", 4096, NULL, 4, &task_wifi_connect_handle) == pdPASS)
    {
        ESP_LOGI(TAG, "task_wifi_connect create success");
        show_spinner();
    }
    else
    {
        ESP_LOGE(TAG, "task_wifi_connect create fail");
    }
}

void action_see_passwd_img_clicked(lv_event_t *e)
{
    extern lv_img_dsc_t img_can_see, img_can_not_see;
    if (lv_textarea_get_password_mode(objects.wifi_passwd_text))
    {
        lv_textarea_set_password_mode(objects.wifi_passwd_text, false);
        lv_img_set_src(objects.see_passwd_img, &img_can_see);
    }
    else
    {
        lv_textarea_set_password_mode(objects.wifi_passwd_text, true);
        lv_img_set_src(objects.see_passwd_img, &img_can_not_see);
    }
}

void action_wifi_passwd_insert(lv_event_t *e)
{

    char *passwd_str = lv_textarea_get_text(objects.wifi_passwd_text);
    ESP_LOGI(TAG, "passwd = %s,size = %d", passwd_str, strlen(passwd_str));
    if (strlen(passwd_str) < 8)
    {
        lv_obj_add_state(objects.connect_wifi_button, LV_STATE_DISABLED);
    }
    else
    {
        lv_obj_clear_state(objects.connect_wifi_button, LV_STATE_DISABLED);
    }
}

void action_switch_to_picture_show_page(lv_event_t *e)
{
    frush_picture_list();
}

void action_http_server_button_clicked(lv_event_t *e)
{
    nvs_write("mode_space","mode_key", 0);
     nvs_deinit();
    vTaskDelay(pdMS_TO_TICKS(100));
    esp_restart();
}

void action_wifi_page_back_button_clicked(lv_event_t *e) {
    nvs_write("mode_space","mode_key", 1);
     nvs_deinit();
    vTaskDelay(pdMS_TO_TICKS(100));
    esp_restart();
}